package com.itmayiedu.controller;

import com.itmayiedu.base.ResponseBase;
import com.itmayiedu.constants.Constants;
import com.itmayiedu.entity.UserEntity;
import com.itmayiedu.feign.MemberServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author cht 【wonderfor@vip.qq.com】
 * @Date 2018/10/24
 */
@Controller
public class RegisterController {

    @Autowired
    private MemberServiceFeign memberServiceFeign;

    private static final String REGISTER = "register";
    private static final String LOGIN = "login";

    //跳转到注册页面
    @GetMapping(value = "/register")
    public String register() {
        return REGISTER;
    }

    //注册业务具体实现
    @PostMapping(value = "/register")
    public String register(UserEntity userEntity, HttpServletRequest request) {
        //1.验证参数
        //2.调用会员注册接口
        userEntity.setCreated(new Date());
        userEntity.setUpdated(new Date());
        ResponseBase regUser = memberServiceFeign.regUser(userEntity);
        //3.如果失败,跳转到失败页面
        if(!regUser.getRtnCode().equals(Constants.HTTP_RES_CODE_200)) {
            request.setAttribute("error", "注册失败");
            return REGISTER;
        }
        //4.如果成功,跳转到成功页面
        return LOGIN;
    }
}
